package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.xml.crypto.Data;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public String login(User user) {
        //1.将密码加密处理
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        //2.查询数据库
        User userDB = userMapper.finUserByUP(user);
        if (userDB == null){
            //用户名和密码不对，返回token应该问null
            return null;
        }
        //说明：用户名和密码正确，返回密钥token
        //     要求密钥唯一
        //    使用uuid(内部hash散列函数)，根据当前毫秒数进行散列，几乎可以保证不重复
        String token = UUID.randomUUID().toString();
        return token;
    }

    /**
     * 业务说明：分页查询
     * sql：select * from user limit 起始的位置，查询的条数
     * 第一页：select * from user limit 0,10 含头不含尾
     * 第二页：select * from user limit 10，10
     * 第三页：select * from user limit 20,10
     * 第N页：select * from user limit (n-1)*10,10
     * */
    @Override
    public PageResult findUserList(PageResult pageResult) {
        int size = pageResult.getPageSize();
        int startNum = (pageResult.getPageNum()-1)*size;
        String query = pageResult.getQuery();
        List<User> userList = userMapper.findUserList(query,size,startNum);
        long total = userMapper.findCount(query);
        return pageResult.setRows(userList).setTotal(total);
    }

    //更新操作，准备修改时间
    @Override
    @Transactional  //如果程序运行出现运行时异常，则出现事务回滚
    public void updateStatus(User user) {
        user.setUpdated(new Date());
        userMapper.updateStatus(user);
    }

    /**
     * 业务思维:
     *      1.密码需要md5加密
     *      2.设定状态信息true
     *      3.设定创建时间和修改时间
     *      4.注意事务的控制
     * */
    @Override
    public void addUser(User user) {
        Date date = new Date();
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password).setStatus(true).setCreated(date).setUpdated(date);
        userMapper.addUser(user);


    }

    @Override
    public User findUserById(Integer id) {
        User user = userMapper.findUserById(id);
        return user;
    }

    @Override
    @Transactional  //增删改注意事务控制
    public void updateUser(User user) {
        user.setUpdated(new Date());
        userMapper.updateUser(user);
    }

    @Override
    @Transactional
    public void deleteUserById(Integer id) {
        userMapper.deleteUserById(id);
    }
}
